from django.shortcuts import render
from rest_framework.views import APIView
from apps.house.models import Area
from django.http import JsonResponse
from apps.house.serializer import AreasSerializers
from apps.house.serializer import HouseSerializers


# Create your views here.

# 城区列表
class AreasView(APIView):
    def get(self, request):
        data = Area.objects.all()
        serializer = AreasSerializers(instance=data, many=True)
        # print(serializer.data)
        return JsonResponse({
            "errmsg": "获取成功",
            "errno": "0",
            "data": serializer.data
        })


# 发布房源
class HouseResourceView(APIView):
    def post(self, request):
        data = request.data
        user_id = request.user.id
        data['user_id'] = user_id
        query = HouseSerializers(data=data)
        if query.is_valid():
            query.save()
            return JsonResponse({
                "errno": "0",
                "errmsg": "发布成功",
                "data": {
                    "house_id": query.data["id"]
                }
            })
        else:
            return JsonResponse({
                "errno": "4103",
                "errmsg": "发布失败",
                "data": {
                    "house_id": 0
                }
            }),

    def get(self, request):
        dict1 = request.GET
        aid = dict1.get('aid')
        sk = dict1.get('sk')
        if aid:
            objs = House.objects.filter(area_id=aid)
        else:
            objs = House.objects.all()
        if sk:
            if sk == 'price-inc':
                objs = objs.order_by('price')
            elif sk == 'price-des':
                objs = objs.order_by('-price')
            else:
                objs = objs.order_by('-order_count')
        list1 = []
        for i in objs:
            list1.append({
                "address": i.address,
                "area_name": i.area.name,
                "ctime": i.create_time,
                "house_id": i.id,
                "img_url": 'http://s5yc6o2h0.hd-bkt.clouddn.com/' + i.index_image_url,
                "order_count": i.order_count,
                "price": i.price,
                "room_count": i.room_count,
                "title": i.title,
                "user_avatar": 'http://s5yc6o2h0.hd-bkt.clouddn.com/' + i.user.avatar
            })
        dict1 = {
            "data": {
                "houses": list1
            },
            "errmsg": "ok",
            "errno": "0"
        }
        return JsonResponse(dict1)


from apps.house.models import House
from utils.qiniuyun import qiniu_upload


# 上传房源图片
class HouseImageView(APIView):
    def post(self, request, house_id):
        house = House.objects.get(id=house_id)
        house_image = request.data.get("house_image")
        # print(house_image)
        # 上传七牛云
        ret, info = qiniu_upload(house_image)
        if info.status_code != 200:
            return JsonResponse({"errno": "4101", "errmsg": "参数错误"})
        house.index_image_url = ret.get('key')
        house.save()
        res = {
            "data": {
                "url": 'http://s5yc6o2h0.hd-bkt.clouddn.com/' + house.index_image_url
            },
            "errno": "0",
            "errmsg": "头像上传成功"
        }
        return JsonResponse(res)

# 首页房屋推荐
class IndexHouseView(APIView):
    def get(self, request):
        houses = House.objects.all()
        list1 = []
        for i in houses:
            list1.append({
                "house_id": i.id,
                "img_url": 'http://s5yc6o2h0.hd-bkt.clouddn.com/' + i.index_image_url,
                "title": i.title
            })
        return JsonResponse({
            "data": list1,
            "errmsg": "OK",
            "errno": "0"
        })

from apps.orders.models import Order
# 房屋详情
class HouseDetailView(APIView):
    def get(self, request, house_id):
        house = House.objects.get(id=house_id)
        if not house:
            return JsonResponse({"errno": "4001", "errmsg": "房屋不存在"})
        list1 = []
        if house.facility.all():
            for i in house.facility.all():
                list1.append(i.id)
        list2 = []
        if house.urls.all():
            for i in house.urls.all():
                list2.append('http://s5yc6o2h0.hd-bkt.clouddn.com/' + i.url)
        order = Order.objects.filter(house_id=house_id)
        list3 = []
        for i in order:
            list3.append({
                    "comment": i.comment,
                    "ctime": i.create_time,
                    "user_name": house.user.username
                })
        dict1 = {

            "acreage": house.acreage,
            "address": house.address,
            "beds": house.beds,
            "capacity": house.capacity,
            "comments": list3,
            "deposit": house.deposit,
            "facilities": list1,
            "hid": house.id,
            "img_urls": list2,
            "max_days": house.max_days,
            "min_days": house.min_days,
            "price": house.price,
            "room_count": house.room_count,
            "title": house.title,
            "unit": house.unit,
            "user_avatar": 'http://s5yc6o2h0.hd-bkt.clouddn.com/' + house.user.avatar,
            "user_id": house.user.id,
            "user_name": house.user.username
        }

        return JsonResponse({
            "data": {
                "house": dict1,
                "user_id": -1 if not request.user else request.user.id
            },
            "errmsg": "OK",
            "errno": "0"
        })
        pass
